﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SearchUtil
{
    class Program
    {
        static ZeneSoft.Engine engine = new ZeneSoft.Engine
        {
            Host = App.Default.Host,
            Port = App.Default.Port,
            Collection = App.Default.Collection
        };


        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0)
                return;

            string arg = args[0];
            char c = arg[0];
            if (c == '/' || c == '-')
            {
                switch (arg.ToLower())
                {
                    case "-help":
                    case "-h":
                    case "-?":
                        ShowHelp();
                        break;

                    case "-ping":
                    case "-p":
                        Console.WriteLine(engine.QueryStatus());
                        break;

                    case "-productcode":
                    case "-code":
                    case "-c":
                        if (args.Length >= 2)
                            Console.WriteLine(engine.GetByCode(args[1]));
                        break;

                    case "-name":
                    case "-n":
                        if (args.Length >= 2)
                            Console.WriteLine(engine.GetByName(args[1]));
                        break;

                    case "-custom":
                        if (args.Length >= 2)

                            Console.WriteLine(engine.GetByCustom(args[1]));
                        break;

                    case "-reindex":
                    case "-r":
                        Console.WriteLine(engine.ReIndex());
                        break;

                    case "-config":
                        if (args.Length >= 2)
                            Console.WriteLine(engine.Config(args[1], ref engine));
                        break;

                    case "-view":
                    case "-v":
                        Helper.OpenWebSite("http://jsonviewer.stack.hu");
                        break;

                    case "-log":
                    case "-l":
                        Helper.ShowTxt(engine.LogFilePath);
                        break;
                }
            }
        }

        static void ShowHelp()
        {
            Console.WriteLine(string.Format("搜索辅助工具。当前引擎信息-主机:{0}端口:{1}实例:{2}", engine.Host, engine.Port, engine.Collection));
            Console.WriteLine(
                       "   -help  -h         显示帮助\n" +

                       "   -ping  -p         状态查询\n" +

                       "   -config -config   配置服务\n" +

                       "   -reindex  -r      重建索引\n" +

                       "   -code    -c       产品名查询\n" +

                       "   -name     -n      关键字查询\n" +

                       "   -view     -v      在线JSON查看\n" +

                       "   -custom           自定义查询\n" +

                       "   -log      -l      打开日志文件");
        }
    }
}
